package cn.tedu.csmall.product.repo.impl;

import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Repository
public class BrandRedisRepositoryImpl implements IBrandRedisRepository {
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void save(BrandStandardVO brandStandardVO) {
        log.debug("开始向Redis写入【BrandStandardVO】数据,参数：{}",brandStandardVO);
        String key = getItemKey(brandStandardVO.getId());
        //存key
        redisTemplate.opsForSet().add(getAllKeys(),key);
        redisTemplate.opsForValue().set(key,brandStandardVO);
    }

    @Override
    public void save(List<BrandListItemVO> brandListItemVOList) {
        log.debug("开始向Redis写入【brandListItemVOList】数据,参数：{}",brandListItemVOList);
        String key = getListKey();
        //存key
        redisTemplate.opsForSet().add(getAllKeys(),key);
        ListOperations<String ,Serializable> ops = redisTemplate.opsForList();
        for (BrandListItemVO itemVO : brandListItemVOList) {
            ops.rightPush(key,itemVO);
        }
    }

    @Override
    public void deleteAll() {
        log.debug("开始从Redis删除所有的【品牌】数据");
        Long count = redisTemplate.delete(getAllKey());
        log.debug("删除所有的【品牌】数据完成，共删除{}个",count);
    }

    @Override
    public BrandStandardVO get(Long id) {
        log.debug("开始从Redis获取品牌数据");
        String key = getItemKey(id);
        Serializable serializable = redisTemplate.opsForValue().get(key);
        if(serializable instanceof BrandStandardVO){
            log.debug("获取完成，BrandStandardVO的参数：{}",serializable);
            return (BrandStandardVO) serializable;
        }
        return null;
    }

    @Override
    public List<BrandListItemVO> list() {
        log.debug("开始从Redis获取品牌列表数据");
        long start = 0;
        long end = -1;
        return list(start,end);
    }

    @Override
    public List<BrandListItemVO> list(long start, long end) {
        String key = getListKey();
        ListOperations<String,Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(key,start,end);
        List<BrandListItemVO> brandListItemVOList = new ArrayList<>();
        for (Serializable serializable : list) {
            if(serializable instanceof BrandListItemVO){
                BrandListItemVO brandListItemVO = (BrandListItemVO) serializable;
                brandListItemVOList.add(brandListItemVO);
            }
        }
        log.debug("获取完成，品牌列表个数：{}",brandListItemVOList.size());
        return brandListItemVOList;
    }

    private String getItemKey(Long id){
        return BRAND_ITEM_KEY_PREFIX + id;
    }

    private String getListKey(){
        return BRAND_LIST_KEY;
    }

    private String getAllKeys(){
        return BRAND_ALL_KEY;
    }

    private Set<String> getAllKey(){
        //同过members方法获取所有的key
        Set<Serializable> members = redisTemplate.opsForSet().members(getAllKeys());
        //遍历所有的key，强转为String类型
        Set<String> keys = new HashSet<>();
        log.debug("获取的key的个数：{}",members.size());
        for (Serializable member : members) {
            if (member instanceof String){
                String key = (String) member;
//                log.debug("key:{}",key);
                keys.add(key);
            }
        }
        return keys;
    }
}
